package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("/page")
    public Result<PageResult> pageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public Result removeById(@RequestParam Integer id){
        categoryService.removeById(id);
        return Result.success();
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    public Result updateOne(@RequestBody CategoryDTO categoryDTO){
        categoryService.updateOne(categoryDTO);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody CategoryDTO dto){
        categoryService.save(dto);
        return Result.success();
    }
    @GetMapping("/list")
    public Result getByType(@RequestParam Integer type){
        List<Category> category=categoryService.getByType(type);
        return Result.success(category);
    }
}
